package acm.蓝桥杯二分前缀;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 非常男女2 {
    static int [] a;
    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        a=new int[n];
        for (int i = 0; i < a.length; i++) {
            a[i]=sc.nextInt();
        }
        System.out.println(count2());
    }
//    //暴力解
//    public static int count(){
//        int max=0;
//        for (int i = 0; i < a.length; i++) {
//            int cur=0;
//            int j=i;
//            for (; j < a.length; j++) {
//                if (a[j]==1){
//                    cur++;
//                }else {
//                    cur--;
//                }
//                if(Math.abs(cur)==2){
//                    j-=2;
//                    break;
//                }
//            }
//            max=Math.max(max,j-i+1);
//        }
//        return max;
//    }

    //暴力解
    public static int count2(){
        int max=0;
        int[] pre =new int[a.length+1];
        if (a[0]==1)pre[0]=1;
        for (int i = 1; i<pre.length; i++) {    //i位置左侧右多少个男生，不包括i
            pre[i]=pre[i-1]+(a[i-1]==1?1:0);
        }
        for (int i = 0; i < pre.length; i++) {  // 每个位置与之对应的偶数为是否符合
            if (a.length-i<=max)break;
            int j=i+max+1;
            for (; j < pre.length-1; j+=2) {
                if (j-i+1==(pre[j+1]-pre[i])*2){ //i-j位置是否有一半为男性，因为是左闭右闭，所以区间人数是j-i+1，因为是求i到
                    max=Math.max(max,j-i+1);
                }
            }
        }
        if (max==1){
            return 0;
        }
        return max;
    }

    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
